package array.binarySearch;

/**
 * 给定一个正整数num，编写一个函数，如果num是一个完全平方数，则返回true，否则返回false 。
 * <p>
 * 进阶：不要使用任何内置的库函数，如sqrt 。
 */
public class N367_ValidPerfectSquare {
    public boolean isPerfectSquare(int num) {
        int left = 0, right = num;
        while (left <= right) {
            int mid = (right - left) / 2 + left;
            if ((long) mid * mid > num) {
                right = mid - 1;
            } else if ((long) mid * mid < num) {
                left = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        N367_ValidPerfectSquare solution = new N367_ValidPerfectSquare();
        System.out.println(solution.isPerfectSquare(15));
    }
}
